/*
 * Selection.cpp
 *
 *  Created on: 2010-10-26
 *      Author: wenjian
 */

#include "Selection.h"

namespace Ed {

  Selection::Selection() {

  }

  Selection::~Selection() {

  }

  /**
   * 处理选择元素事件
   */
  bool Selection::handle(const Thk::Event* e){
    SelectEvent* se = (SelectEvent*)e;
    Element* el = (Element*)se->getSource();
    if(el->isSelected()){
      Elements::add(el);
    }else{
      Elements::remove(el);
    }
    return false;
  }


  /**
   * 加入元素
   */
  bool Selection::add(AbstractElement* e, int pos){
    if(!e->isSelected()){
      e->setSelected(true);
      Elements::add(e, pos); //调整位置
      return true;
    }else{
      return Elements::add(e, pos);
    }
  }


  /**
   * 移除元素
   */
  bool Selection::remove(AbstractElement* e){
    if(!e){
      return false;
    }
    if(e->isSelected()){
      e->setSelected(false);
      return true;
    }else{
      return Elements::remove(e);
    }
  }


  /**
   * 清除所有元素
   */
  void Selection::clear(){
    Elements::clearAndExcludes(NULL);
  }

  /**
   * 加入除excluded以外的所有元素
   */
  void Selection::addAllAndExclude(const std::vector<AbstractElement*>* els, const AbstractElement* excluded){
    if(!els->empty()){
      std::vector<AbstractElement*>::const_iterator it = els->begin();
      for(; it != els->end(); it++){
        AbstractElement* e = *it;
        if(excluded!=NULL && e == excluded){
          continue;
        }
        add(e);
      }
      //Elements::destroyBoundRect();
      setSortFlag(false);
    }
  }


  std::vector<AbstractElement*>* Selection::removesAndExclude(vector<AbstractElement*>* v, const AbstractElement* excluded){
    if(this->elements.empty()){
      return v;
    }
    size_t count = this->elements.size();
    for (std::vector<AbstractElement*>::iterator it = elements.begin(); it != elements.end();) {
      if(excluded==NULL || (*it) != excluded){
        AbstractElement* e = *it;
        if(v){
          v->push_back(e);
        }
        this->elements.erase(it);
        if(e->isSelected()){
          e->setSelected(false);
        }
      }else{
        it++;
      }
    }
    if(this->elements.size()!=count){
      Elements::setSortFlag(false);
//      Elements::destroyBoundRect();
    }
    return v;
  }

  std::vector<AbstractElement*>* Selection::removesAndExcludes(vector<AbstractElement*>* v, const std::vector<AbstractElement*>* excludeds){
    if(this->elements.empty()){
      return v;
    }
    size_t count = this->elements.size();
    for (std::vector<AbstractElement*>::iterator it = elements.begin(); it != elements.end();) {
      if(excludeds==NULL || !std::binary_search(excludeds->begin(), excludeds->end(), *it)){
        AbstractElement* e = *it;
        if(v){
          v->push_back(e);
        }
        this->elements.erase(it);
        if(e->isSelected()){
          e->setSelected(false);
        }
      }else{
        it++;
      }
    }
    if(this->elements.size()!=count){
      Elements::setSortFlag(false);
//      Elements::destroyBoundRect();
    }
    return v;
  }


}
